package com.astrob.adapters;


import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.model.FavoriteFileHandle;
import com.astrob.model.HistoryData;
import com.astrob.model.HistoryFileHandle;
import com.astrob.nhcmap.R;

public class PlanHistoryAdapter extends BaseAdapter {
	static class ViewHolder{
		TextView text;
		ImageButton addFav;
	}
	Context mContext = null;
	public PlanHistoryAdapter(Context context){
		mContext = context;
	}
	@Override
	public int getCount() {
		return 1+ HistoryFileHandle.getInstance().getNativeHistory().size();
	}

	@Override
	public Object getItem(int position) {
		if(position == 0){
			return new HistoryData();
		}
		else{
			return HistoryFileHandle.getInstance().getNativeHistory().get(position-1);
		}

	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public interface HistoryItemListener{
		//当只有起点时，不能切换
		public void onAddFav(HistoryData data);
		public void onDelFav(HistoryData data);
		public void onItem(HistoryData data);
		public void onEmpty();
	}
	HistoryItemListener mListener = null;
	public void setListener(HistoryItemListener listener){
		mListener = listener;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if (null == convertView) {
			convertView = LinearLayout.inflate(mContext,
					R.layout.adapter_routeplan_history, null);

			holder = new ViewHolder();
			holder.text = (TextView) convertView.findViewById(R.id.history_text);
			holder.addFav = (ImageButton)convertView.findViewById(R.id.history_fav);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(position == 0){
			holder.text.setTextColor(0xff3b90df);
			holder.text.setText("历史记录");
			holder.addFav.setBackgroundResource(R.drawable.selector_btn_route_empty);
			holder.addFav.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(mListener != null){
						mListener.onEmpty();
					}
				}
			});
			int count = HistoryFileHandle.getInstance().getNativeHistory().size();
			if(count == 0){
				holder.addFav.setVisibility(View.INVISIBLE);
			}else{
				holder.addFav.setVisibility(View.VISIBLE);
			}

		}else{
			final HistoryData data = HistoryFileHandle.getInstance().getNativeHistory().get(position-1);
			holder.text.setText(data.name);
			final boolean isFav = FavoriteFileHandle.getInstance().isHistInFav(data);
			if(isFav){
				holder.addFav.setBackgroundResource(R.drawable.route_fav_d);
			} else{
				holder.addFav.setBackgroundResource(R.drawable.route_fav_u);
			}
			holder.addFav.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(mListener != null){
						if(isFav){
							mListener.onDelFav(data);
						} else{
							mListener.onAddFav(data);
						}						
						
					}
				}
			});
			holder.text.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(mListener != null){
						mListener.onItem(data);
					}
					
				}
			});
		}
		return convertView;
	}

}
